package com.xasz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xasz.entity.User;
import com.xasz.mapper.UserMapper;
import com.xasz.service.IUserService;
import com.xasz.service.ex.NotNxistException;
import com.xasz.service.ex.RepeatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    /**
     * 简单的注册
     */
    @Override
    public void reg(String username, String password) {
        //判断是否有重复用户
        QueryWrapper<User> wrapper=new QueryWrapper<User>().eq("username",username);
        User users=userMapper.selectOne(wrapper);
        if (users!=null){
            throw new RepeatException("用户名已存在");
        }
        //创建用户
        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        userMapper.insert(user);
    }

    /**
     * 简单的登录
     */
    @Override
    public User login(String username, String password) {
        QueryWrapper<User> wrapper=new QueryWrapper<User>()
                .eq("username",username)
                .eq("password",password);
        if(userMapper.selectOne(wrapper)==null){
            throw new NotNxistException("账号或密码错误");
        }
        return userMapper.selectOne(wrapper);
    }
}
